package com.mifengkong.kotlincommon.recyclerview.refreshload.refresh

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import com.mifengkong.kotlincommon.R

/**
 * Created by jack on 2018/6/20
 */
class DefaultRefreshView : RefreshViewCreator() {

    private lateinit var tvRefreshView: TextView
    private lateinit var ivArrow: ImageView
    private lateinit var progressBar: ProgressBar
    private var isArrowUp: Boolean = false
    private var mFlipAnimation: RotateAnimation? = null
    private var mReverseFlipAnimation: RotateAnimation? = null
    private val mRotateAniTime = 150

    override fun getRefreshView(context: Context, parent: ViewGroup): View {
        val refreshView = LayoutInflater.from(context).inflate(R.layout.default_refresh_header, parent, false)
        tvRefreshView = refreshView.findViewById(R.id.drh_tv_refresh_view)
        ivArrow = refreshView.findViewById(R.id.drh_iv_arrow)
        progressBar = refreshView.findViewById(R.id.drh_progress)
        buildAnimation()
        return refreshView
    }

    private fun buildAnimation() {
        mFlipAnimation = RotateAnimation(0f, -180f, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f)
        mFlipAnimation?.let {
            it.interpolator = LinearInterpolator()
            it.duration = mRotateAniTime.toLong()
            it.fillAfter = true
        }

        mReverseFlipAnimation = RotateAnimation(-180f, 0f, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f)
        mReverseFlipAnimation?.let {
            it.interpolator = LinearInterpolator()
            it.duration = mRotateAniTime.toLong()
            it.fillAfter = true
        }
    }

    override fun onDropDown(currentDragHeight: Int, refreshViewHeight: Int, currentRefreshState: Int) {
        val rotate = currentDragHeight.toFloat() / refreshViewHeight
        ivArrow.visibility = View.VISIBLE
        progressBar.visibility = View.GONE
        if (rotate >= 0 && currentRefreshState == DropDownRefreshRecyclerView.REFRESH_STATUS_LOOSEN_REFRESHING && !isArrowUp) {
            isArrowUp = true
            tvRefreshView.text = "释放刷新"
            ivArrow.clearAnimation()
            ivArrow.startAnimation(mFlipAnimation)
        } else if (rotate < 0 && currentRefreshState == DropDownRefreshRecyclerView.REFRESH_STATUS_PULL_DOWN_REFRESH && isArrowUp) {
            isArrowUp = false
            ivArrow.clearAnimation()
            ivArrow.startAnimation(mReverseFlipAnimation)
            tvRefreshView.text = "下拉刷新"
        }
    }

    override fun onRefreshing() {
        ivArrow.clearAnimation()
        ivArrow.visibility = View.GONE
        progressBar.visibility = View.VISIBLE
        tvRefreshView.text = "正在刷新..."
    }

    override fun onStopRefresh() {
        ivArrow.visibility = View.GONE
        progressBar.visibility = View.GONE
        tvRefreshView.text = "刷新完成"
    }

    override fun restoreRefresh() {
        tvRefreshView.text = "下拉刷新"
        if (isArrowUp) {
            isArrowUp = false
            ivArrow.clearAnimation()
            ivArrow.startAnimation(mReverseFlipAnimation)
        }
    }

}